package com.etc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.entity.Dept;
import com.etc.entity.Employees;
import com.etc.entity.Records;
import com.etc.service.IDeptService;
import com.etc.service.IRecordsService;
import com.etc.service.impl.DeptServiceImpl;
import com.etc.service.impl.EmployeesServiceImpl;
import com.etc.service.impl.RecordsServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class AttenController
 */
@WebServlet("/AttenController")
public class AttenController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	 //调用业务层
	EmployeesServiceImpl service=new EmployeesServiceImpl();
	IRecordsService service1=new RecordsServiceImpl();
	IDeptService service2=new DeptServiceImpl();
      
   /**
    * @see HttpServlet#HttpServlet()
    */
   public AttenController() {
       super();
       // TODO Auto-generated constructor stub
   }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置请求编码,位置需要放到获取值的前面
	    request.setCharacterEncoding("utf-8");
	    //设置响应编码
	    response.setCharacterEncoding("utf-8");	    
	    String op=request.getParameter("op");
	  
	    if("vaguesf".equals(op)) {
			vaguesf(request, response);				//后台考勤管理比对员工信息
					
		}else if("addReco".equals(op)) {
			addReco(request, response);				//后考勤管理页添加记录（补卡）

		/*}else if("vague".equals(op)) {
			vague(request, response);*/				//后台考勤管理显示简略记录

		}else if("vaguefs".equals(op)) {
			vaguefs(request, response);				//后台考勤管理显示模态框详情记录

		}else if("dels".equals(op)) {
			dels(request, response);				//后台考勤管理删除详情记录

		}else if("modifs".equals(op)) {
			modifs(request, response);				//后台考勤管修改详情记录

		}else if("vaguesd".equals(op)) {
			vaguesd(request, response);				//后台考勤管理分页显示

		}
			
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	private void vaguesf(HttpServletRequest request, HttpServletResponse response) throws IOException {	
		int empId=Integer.parseInt(request.getParameter("empId"));
		Employees employees=service.queryById(empId);
		Gson gson=new Gson();
		String json=gson.toJson(employees);
		response.getWriter().print(json);
		response.getWriter().close();
	}
	//后员工管理页添加员工
		protected void addReco(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			int empId=Integer.parseInt(request.getParameter("empId"));
			String jsonString= request.getParameter("record");
			Gson gson=new Gson();			
			Records record=gson.fromJson(jsonString, Records.class);
		     boolean flag=service1.addRecords(record, empId);    	
				String json=gson.toJson(flag);
				response.getWriter().print(json);
				response.getWriter().close();						
		}
		/*private void vague(HttpServletRequest request, HttpServletResponse response) throws IOException {			
			List<Statistics> statistics=service1.getqueryAllRecords();
			Gson gson=new Gson();
			String json=gson.toJson(statistics);
			response.getWriter().print(json);
			response.getWriter().close();
		}*/
		private void vaguefs(HttpServletRequest request, HttpServletResponse response) throws IOException {	
			int empId=Integer.parseInt(request.getParameter("empId"));
			String beginTime=request.getParameter("begin");
			String endTime=request.getParameter("end");
			int clockIn=Integer.parseInt(request.getParameter("clockIt"));	
			List<Records> records=service1.getqueryById(empId, beginTime, endTime,clockIn);
			Gson gson=new Gson();
			String json=gson.toJson(records);
			response.getWriter().print(json);
			response.getWriter().close();
		}
		private void dels(HttpServletRequest request, HttpServletResponse response) throws IOException {	
			int recordId=Integer.parseInt(request.getParameter("recordId"));
			boolean record=service1.delRecords(recordId);
			Gson gson=new Gson();
			String json=gson.toJson(record);
			response.getWriter().print(json);
			response.getWriter().close();
		}
		protected void modifs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String jsonString= request.getParameter("record");		
			Gson gson=new Gson();			
			Records record=gson.fromJson(jsonString, Records.class);				
			boolean flag=service1.updateRecords(record);		  
			String json=gson.toJson(flag);
			response.getWriter().print(json);
			response.getWriter().close();							
		}
		
		private void vaguesd(HttpServletRequest request, HttpServletResponse response) throws IOException {		
			int pageNo=1; //当前页						
			if (request.getParameter("pageNo")!=null) {
				pageNo=Integer.parseInt(request.getParameter("pageNo"));
			}		
			int pageSize=Integer.parseInt(request.getParameter("pageSize"));
			String begintime= request.getParameter("begintime");
			String endtime= request.getParameter("endtime");
			int empId=Integer.parseInt(request.getParameter("reId"));
			String empName= request.getParameter("empName");		
			int clockIn=Integer.parseInt(request.getParameter("clockIn"));		
			String deptName= request.getParameter("deptName");
			int deptId=0;			
			List<Dept> list = service2.getqueryDept();
			System.out.println("deptName:" + deptName);
			if (deptName!=""){
				for (Dept article : list) {					
					if (deptName.equals(article.getDeptName())) {
						deptId=article.getDeptId();
						Gson gson=new Gson();
						System.out.println(deptId);						
						PageData pageData=service1.queryKeyRecords(pageNo, pageSize,begintime,endtime,empId,clockIn,deptId,empName);				
						String json=gson.toJson(pageData);
						response.getWriter().print(json);
						response.getWriter().close();
					}			
				}
			}
			if(deptName==""){
				Gson gson=new Gson();
				PageData pageData=service1.queryKeyRecords(pageNo, pageSize,begintime,endtime,empId,clockIn,deptId,empName);				
				String json=gson.toJson(pageData);
				response.getWriter().print(json);
				response.getWriter().close();
			}				
		}
		
}
